﻿$(window).load(function () {
    $('.ExperimentParametersChildrenCheckBox > input').change(function () {
        var showChildren = $(this).is(':checked');
        ToggleShowChildren(showChildren);
    });

    $('.ExperimentParametersSystemCheckBox > input').change(function () {
        var showSystem = $(this).is(':checked');
        ToggleShowSystem(showSystem);
    });

    //Add some JavaScript to the page to hide the ListView (this will only be added if there are child controls)
    $('.ChildParametersShowLabel').live('click', function () {
        var currentlyHidden = $(this).html() == '+';
        var grid = $(this).closest('tr').next('tr').find('div.SubExperimentParametersGrid:first');
        grid.toggle(currentlyHidden);
        var labelnewval = currentlyHidden ? '&mdash;' : '+';
        $(this).html(labelnewval);
    });

    //Make rows draggable
    $('.parameterrow').draggable({
        handle: '.dragpanel',
        revert: 'invalid',
        helper: function (event) {
            return $('<div><table></table></div>').find('table').append($(event.target).closest('tr').clone()).appendTo('body');
        }
    });

    //Make rows droppable, and define what to do when a row is dropped
    $('.parameterrow').droppable({
        hoverClass: "DroppableDivHover",
        drop: function (event, ui) {
            var parentparamid = ui.draggable.find('input.ParentParamID:first').val();
            $(this).find('input.NewChildParamID:first').get(0).value = parentparamid;
            $(this).find('a.AssociateLinkButton').click();
        }
    });

    $('.ParameterPositionInsert').droppable({
        hoverClass: "DroppableParameterPositionInsert",
        drop: function (event, ui) {
            var paramIDToInsert = ui.draggable.find('input.ParamIDToDemote:first').val();
            $(this).next('tr').find('input.ParamIDToInsert:first').get(0).value = paramIDToInsert;
            $(this).next('tr').find('a.ParameterPositionButton').click();
        }
    });

    var showChildren = $('.ExperimentParametersChildrenCheckBox > input').is(':checked');
    ToggleShowChildren(showChildren);
});

function ToggleShowChildren(showChildren) {
    if (showChildren) {
        $('.SubExperimentParametersGrid').show();
        $('.ChildParametersShowLabel').html('&mdash;');
    }
    else {
        $('.SubExperimentParametersGrid').hide();
        $('.ChildParametersShowLabel').html('+');
    }
}

function ToggleShowSystem(showSystem) {
    if (showSystem) {
        $('.SystemParameterRow').show();
    }
    else {
        $('.SystemParameterRow').hide();
    }
}
